//package health_system.health_system.service;
//
//import health_system.health_system.entity.ChatMessage;
//import health_system.health_system.entity.ChatRoom;
//import health_system.health_system.entity.User;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//
//import org.springframework.stereotype.Service;
//
//@Service
//@RequiredArgsConstructor
//@Slf4j
//public class NotificationService {
//
//    /**
//     * Send notification when an appointment is created
//     */
//    public void sendAppointmentCreationNotification(Appointment appointment) {
//        // In a real implementation, this would send email/SMS/push notifications
//        log.info("Sending appointment creation notification for appointment ID: {}", appointment.getId());
//
//        // Notify the doctor
//        log.info("Notification to doctor (ID: {}): New appointment request from patient (ID: {}) on {}",
//                appointment.getDoctor().getId(),
//                appointment.getPatient().getId(),
//                appointment.getStartTime());
//
//        // Notify the patient
//        log.info("Notification to patient (ID: {}): Appointment request with Dr. {} created for {}",
//                appointment.getPatient().getId(),
//                appointment.getDoctor().getId(),
//                appointment.getStartTime());
//    }
//
//    /**
//     * Send notification when an appointment is updated
//     */
//    public void sendAppointmentUpdateNotification(Appointment appointment) {
//        log.info("Sending appointment update notification for appointment ID: {}", appointment.getId());
//
//        // Notify both parties about the update
//        String statusInfo = appointment.getStatus().toString();
//
//        // Notify the doctor
//        log.info("Notification to doctor (ID: {}): Appointment with patient (ID: {}) has been updated. Status: {}",
//                appointment.getDoctor().getId(),
//                appointment.getPatient().getId(),
//                statusInfo);
//
//        // Notify the patient
//        log.info("Notification to patient (ID: {}): Your appointment with Dr. {} has been updated. Status: {}",
//                appointment.getPatient().getId(),
//                appointment.getDoctor().getId(),
//                statusInfo);
//    }
//
//    /**
//     * Send notification when an appointment is confirmed
//     */
//    public void sendAppointmentConfirmation(Appointment appointment) {
//        log.info("Sending appointment confirmation notification for appointment ID: {}", appointment.getId());
//
//        // Notify the doctor
//        log.info("Notification to doctor (ID: {}): Appointment with patient (ID: {}) on {} has been confirmed",
//                appointment.getDoctor().getId(),
//                appointment.getPatient().getId(),
//                appointment.getStartTime());
//
//        // Notify the patient
//        log.info("Notification to patient (ID: {}): Your appointment with Dr. {} on {} has been confirmed",
//                appointment.getPatient().getId(),
//                appointment.getDoctor().getId(),
//                appointment.getStartTime());
//    }
//
//    /**
//     * Send notification when an appointment is cancelled
//     */
//    public void sendAppointmentCancellation(Appointment appointment) {
//        log.info("Sending appointment cancellation notification for appointment ID: {}", appointment.getId());
//
//        // Extract cancellation reason if available
//        String reason = "";
//        if (appointment.getDoctorNote() != null && appointment.getDoctorNote().contains("Cancelled. Reason:")) {
//            reason = appointment.getDoctorNote().replace("Cancelled. Reason:", "").trim();
//        }
//
//        // Notify the doctor
//        log.info("Notification to doctor (ID: {}): Appointment with patient (ID: {}) on {} has been cancelled. Reason: {}",
//                appointment.getDoctor().getId(),
//                appointment.getPatient().getId(),
//                appointment.getStartTime(),
//                reason);
//
//        // Notify the patient
//        log.info("Notification to patient (ID: {}): Your appointment with Dr. {} on {} has been cancelled. Reason: {}",
//                appointment.getPatient().getId(),
//                appointment.getDoctor().getId(),
//                appointment.getStartTime(),
//                reason);
//    }
//
//    /**
//     * Send reminder notification for upcoming appointment
//     */
//    public void sendAppointmentReminder(Appointment appointment) {
//        log.info("Sending appointment reminder notification for appointment ID: {}", appointment.getId());
//
//        // Calculate hours until appointment
//        long hoursUntilAppointment = java.time.Duration.between(
//                java.time.LocalDateTime.now(),
//                appointment.getStartTime()).toHours();
//
//        // Notify the doctor
//        log.info("Reminder to doctor (ID: {}): You have an appointment with patient (ID: {}) in {} hours",
//                appointment.getDoctor().getId(),
//                appointment.getPatient().getId(),
//                hoursUntilAppointment);
//
//        // Notify the patient
//        log.info("Reminder to patient (ID: {}): You have an appointment with Dr. {} in {} hours",
//                appointment.getPatient().getId(),
//                appointment.getDoctor().getId(),
//                hoursUntilAppointment);
//
//        if (appointment.getIsOnline() && appointment.getMeetingLink() != null) {
//            log.info("Meeting link: {}", appointment.getMeetingLink());
//        } else if (appointment.getLocation() != null) {
//            log.info("Location: {}", appointment.getLocation());
//        }
//    }
//
//    /**
//     * 发送聊天消息通知
//     * 当收到新消息时通知用户
//     */
//    public void sendChatMessageNotification(User recipient, ChatMessage message) {
//        log.info("发送聊天消息通知给用户 ID: {}, 消息 ID: {}", recipient.getId(), message.getId());
//
//        // 获取发送者信息
//        String senderName = message.getSender().getUsername();
//
//        // 获取消息预览
//        String contentPreview;
//        switch (message.getMessageType()) {
//            case TEXT:
//                String text = message.getTextContent();
//                contentPreview = text.length() > 30 ? text.substring(0, 30) + "..." : text;
//                break;
//            case IMAGE:
//                contentPreview = "[图片]";
//                break;
//            case VIDEO:
//                contentPreview = "[视频]";
//                break;
//            case AUDIO:
//                contentPreview = "[语音]";
//                break;
//            case FILE:
//                contentPreview = "[文件]";
//                break;
//            default:
//                contentPreview = "[新消息]";
//                break;
//        }
//
//        // 获取聊天室信息
//        String chatRoomInfo = message.getChatRoom().getChatType().toString();
//        if (message.getChatRoom().getChatType() == ChatRoom.ChatType.GROUP) {
//            chatRoomInfo = message.getChatRoom().getGroupName();
//        }
//
//        // 模拟发送通知
//        log.info("CHAT_NOTIFICATION -> 接收者: {}, 发送者: {}, 聊天: {}, 内容: {}",
//                recipient.getUsername(), senderName, chatRoomInfo, contentPreview);
//
//        // 实际实现可能使用WebSocket、推送通知、短信等
//    }
//
//    /**
//     * 发送聊天消息已读通知
//     * 当消息被读取时通知发送者
//     */
//    public void sendMessageReadNotification(User reader, ChatMessage message) {
//        // 只向发送者通知
//        User sender = message.getSender();
//        log.info("发送消息已读通知给用户 ID: {}, 消息已被用户 ID: {} 阅读", sender.getId(), reader.getId());
//
//        // 模拟发送通知
//        log.info("READ_NOTIFICATION -> 发送者: {}, 消息已被 {} 在 {} 阅读",
//                sender.getUsername(), reader.getUsername(), java.time.LocalDateTime.now());
//    }
//
//    /**
//     * 发送聊天室活动通知
//     * 用于通知群聊中的事件（如新成员加入、成员离开等）
//     */
//    public void sendChatRoomActivityNotification(ChatRoom chatRoom,
//                                                 String activityType, User actor, User target) {
//        // 只对群聊有效
//        if (chatRoom.getChatType() != ChatRoom.ChatType.GROUP) {
//            return;
//        }
//
//        String activityInfo;
//        switch (activityType) {
//            case "JOIN":
//                activityInfo = actor.getUsername() + " 邀请了 " + target.getUsername() + " 加入群聊";
//                break;
//            case "LEAVE":
//                activityInfo = target.getUsername() + " 离开了群聊";
//                break;
//            case "REMOVE":
//                activityInfo = actor.getUsername() + " 将 " + target.getUsername() + " 移出了群聊";
//                break;
//            case "ADMIN":
//                activityInfo = actor.getUsername() + " 将 " + target.getUsername() + " 设为了管理员";
//                break;
//            default:
//                activityInfo = "群聊信息已更新";
//                break;
//        }
//
//        log.info("GROUP_ACTIVITY -> 群聊: {}, 活动: {}", chatRoom.getGroupName(), activityInfo);
//
//        // 向所有群成员发送通知
//        for (User participant : chatRoom.getParticipants()) {
//            if (!participant.getId().equals(actor.getId())) {
//                log.info("向用户 {} 发送群聊活动通知", participant.getUsername());
//            }
//        }
//    }
//}